package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.Bean;
import com.sikao.app.bean.SendMailSubmitBean;
import com.sikao.app.dao.SendMailModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.AndroidBug5497Workaround;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.StringUtils;

/**
 * 发送站内信
 */
public class SendMailActivity extends BaseActivity implements View.OnClickListener{

	private TextView titleTv;
	private ImageView backIv;
	private EditText accountNumberEt;
	private EditText titleEt;
	private EditText contentEt;
	private TextView sendTv;
	private String type;
	private String name;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_send_mail);
		AndroidBug5497Workaround.assistActivity(this);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		Intent intent = getIntent();
		type = intent.getStringExtra("type");
		name = intent.getStringExtra("value");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		accountNumberEt = (EditText) findViewById(R.id.account_number_et);
		titleEt = (EditText) findViewById(R.id.title_et);
		contentEt = (EditText) findViewById(R.id.content_et);
		sendTv = (TextView) findViewById(R.id.title_right_tv);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
		sendTv.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("发送站内信");
		sendTv.setText("发送");
		sendTv.setVisibility(View.VISIBLE);
		backIv.setVisibility(View.VISIBLE);
		if (type != null && type.length() != 0){
			accountNumberEt.setText(name);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()){
			case R.id.title_left_img:
				finish();
				break;
			case R.id.title_right_tv:
				sendMail();
				break;
			default:
				break;
		}
	}

	private void sendMail(){
		if(!checkInput()){
			return;
		}
		SendMailSubmitBean bean = new SendMailSubmitBean();
		bean.setTitle(titleEt.getText().toString());
		bean.setCont(contentEt.getText().toString());
		if (type != null && type.equals("name")){
			bean.setToType("name");
		}else if (type != null && type.equals("phone")){
			bean.setToType("phone");
		}else {
			if (StringUtils.isMobile(accountNumberEt.getText().toString())){
				bean.setToType("phone");
			}else{
				bean.setToType("name");
			}
		}
		bean.setTo(accountNumberEt.getText().toString());
		new SendMailModel(this, new OnNetBackListener<Bean>() {
			@Override
			public void netBack(Bean data) {
				if (data == null){
					return;
				}
				if (data.getStatus().equals("1")){
					Toast.makeText(SendMailActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
					finish();
				}else{
					Toast.makeText(SendMailActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				}
			}
		}).request(JSONUtil.toJSON(bean),true);
	}

	private boolean checkInput(){
		if(TextUtils.isEmpty(accountNumberEt.getText().toString())){
			Toast.makeText(this, "请输入对方账号", Toast.LENGTH_SHORT).show();
			return false;
		}
		if(TextUtils.isEmpty(titleEt.getText().toString())){
			Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
			return false;
		}
		if(TextUtils.isEmpty(contentEt.getText().toString())){
			Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

}
